﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClinicalMassageSystem
{
    public partial class PatientProfile : Form
    {
        bool promptSave = false;

        public PatientProfile():this(0)
        {
        }

        //Open in Add mode if i == 1
        public PatientProfile(int i)
        {
            InitializeComponent();
            this.CancelButton = btnCancel;

            patientBindingSource.DataSource =
                    from patient in cms.Patients
                    orderby patient.LastName
                    select patient;

            if (i == 1)
            {
                patientBindingSource.AddNew();
                btnPreviousRecord.Visible = false;
                btnNextRecord.Visible = false;
            }
            else
            {
                patientBindingSource.MoveFirst();
            }

            this.btnSave.Enabled = false;
        }

        //LINQ to SQL data context
        private CMSDataContext cms = new CMSDataContext();

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtFirstName.TextLength.ToString() == "0" || txtLastName.TextLength.ToString() == "0")
                {
                    MessageBox.Show("First Name and Last Name must be filled!");
                }
                else
                {
                    Validate();
                    patientBindingSource.EndEdit();
                    cms.SubmitChanges();
                    lblStatus.Text = "Save Successful";
                    promptSave = false;
                }
            }
            catch(Exception ex){
                MessageBox.Show(ex.Message.ToString());
            }

            this.btnSave.Enabled = false;
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();    
        }

        private void fieldChanged(object sender, EventArgs e)
        {
            this.btnSave.Enabled = true;
            this.lblStatus.Text = "Edit in progress...";
            promptSave = true;
        }

        private void promptForSave(object sender, EventArgs e)
        {
            if (promptSave)
            {
                DialogResult q = MessageBox.Show("You have not saved your current record. Would you like to save?", "Unsaved Information", MessageBoxButtons.YesNo);
                if (q == DialogResult.Yes)
                {
                    btnSave_Click(sender, e);
                    promptSave = false;
                }
            }
        }

        private void btnNewPatient_Click(object sender, EventArgs e)
        {
            try
            {
                promptForSave(sender, e);
                patientBindingSource.AddNew();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnNextRecord_Click(object sender, EventArgs e)
        {
            patientBindingSource.MoveNext();
        }

        private void btnPreviousRecord_Click(object sender, EventArgs e)
        {
            patientBindingSource.MovePrevious();
        }

        private void PatientProfile_Load(object sender, EventArgs e)
        {
            this.btnSave.Enabled = false;
        }

        private void patientBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void PatientProfile_FormClosed(object sender, FormClosedEventArgs e)
        {
            promptForSave(sender, e);
        }
    }
}
